package com.guaoran.interview.spring.autowired.controller;

import com.guaoran.interview.spring.autowired.service.QualifierService;
import com.guaoran.interview.spring.autowired.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author : 孤傲然
 * @description : {@link Resource}
 * @see AutowiredController
 * @desc : {@link Resource} 可以声明在字段、单个参数的方法上，默认是根据名称进行注入
 * @desc : {@link Autowired} 适用于字段，构造函数、单个、多个参数的方法中，从而允许在参数级别缩小限定符注释的范围
 * @desc : 相反，{@link Resource} 仅支持单个参数的方法和bean属性set 方法，
 * @desc : 因此，如果注入目标是构造函数活多参数方法，适用于{@link Autowired}
 * @date :2020/1/3 17:38
 */
@RestController
public class ResourceController {
//    @Resource
    private QualifierService qualifierService;
//    @Resource
    private TransactionService transactionService;

//    @Resource
//    public void setParam(QualifierService qualifierService,TransactionService transactionService){
//        System.out.println("resource set 注入，多个参数， 会报错：@Resource annotation require a single-arg method ");
//        this.qualifierService = qualifierService;
//        this.transactionService = transactionService;
//    }

    @Resource(name = "secondQualifierService")
    public void setParam(QualifierService qualifierService){
//        System.out.println("resource set 注入， 一个参数 qualifierService");
        this.qualifierService = qualifierService;
    }

    @Resource
    public void setParam(TransactionService transactionService){
//        System.out.println("resource set 注入， 一个参数 transactionService");
        this.transactionService = transactionService;
    }

    @GetMapping("/resource")
    public String resource(){
        return qualifierService.toString()+"--"+transactionService;
    }
}
